//package com.wangc.commons.interceptor.handler;
//
//import cn.com.jsepc.appframe.system.AppframeOrgClient;
//import cn.com.jsepc.appframe.webservice.client.AppframeClientConfig;
//import com.alibaba.fastjson.JSON;
//import com.dlxx.authMode.sso.SsoUserInfo;
//import com.google.gson.Gson;
//import com.jsepc.portal.sso.DES;
//import com.wangc.commons.constants.CommonConstants;
//import com.wangc.commons.dto.User;
//import com.wangc.commons.enums.RedisKeyEnum;
//import com.wangc.commons.utils.DOConverter;
//import com.wangc.commons.utils.SHA256Util;
//import com.wangc.commons.constants.RedisConstants;
//import com.wangc.commons.utils.RedisKeyUtils;
//import com.wangc.commons.utils.RedisUtils;
//import com.wangc.commons.interceptor.UserThreadLocal;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.http.Cookie;
//import javax.servlet.http.HttpServletRequest;
//import java.net.URLDecoder;
//
///**
// * @program: do-yx-core
// * @description: 获取用户详情逻辑
// * @author: Ashin
// * @create: 2020-05-15 13:52
// **/
//@Slf4j
//@Component
//public class UserTaskHandler {
//
//    @Value("${conf.app.client.host:null}")
//    private String appClientHost;
//
//    /**
//     * 从redis中获取用户信息，添加到ThreadLocal
//     *
//     * @return
//     */
//    public void getUserFromRedisCache(HttpServletRequest servletRequest) {
//        log.info("--[get user time consuming]-- start to get user cookie");
//        long getUserCookieTime = System.currentTimeMillis();
//        String userCookie = getUserCookie(servletRequest);
//        log.info("--[get user time consuming]-- end to get user cookie,wasted {} ms",System.currentTimeMillis() - getUserCookieTime);
//        if (StringUtils.isEmpty(userCookie)) {
//            log.warn("User cookie is null , Failed to retrieve user information!");
//            return;
//        }
//        try {
//            String sha256UserKey = SHA256Util.getSHA256String(userCookie);
//            User user = RedisUtils.getBean(RedisKeyUtils.getRedisKey(RedisKeyEnum.COMMONS_USER.key(), sha256UserKey), User.class);
//            if (null == user) {
//                user = getUserByAppFrameProxy(userCookie, sha256UserKey);
//            }
//            UserThreadLocal.setUser(user);
//        } catch (Exception e) {
//            log.error("Failed to retrieve user information!", e);
//        }
//
//    }
//
//    /**
//     * 从head获取用户信息
//     * @param servletRequest
//     */
//    public void getUserFromHead(HttpServletRequest servletRequest) {
//        try {
//            String userStr = servletRequest.getHeader("user");
//            if (StringUtils.isEmpty(userStr)) {
//                log.warn("User cookie is null , Failed to retrieve user information!");
//                return;
//            }
//            String user = URLDecoder.decode(userStr,"utf-8");
//            UserThreadLocal.setUser(JSON.parseObject(user, User.class));
//        } catch (Exception e) {
//            log.error("parse json error !", e);
//        }
//
//    }
//
//    /**
//     * 通过统一权限平台获取用户信息
//     *
//     * @param cookie
//     * @param sha256UserKey 用户凭证 cookie
//     * @return
//     */
//    private User getUserByAppFrameProxy(String cookie, String sha256UserKey) throws Exception {
//        //通过request获取用户id
//        log.info("--[get user time consuming]-- start to decrypt user cookie");
//        long decryptTime = System.currentTimeMillis();
//        String userId = (new Gson()).fromJson(DES.decryptStr(cookie), SsoUserInfo.class).getUserId();
//        log.info("--[get user time consuming]-- end to decrypt user cookie,wasted {} ms",System.currentTimeMillis() - decryptTime);
//        log.info("--[get user time consuming]-- start to get user from appframeClient");
//        log.info("--[appClientHost]-- " + appClientHost);
//        long getUserTime = System.currentTimeMillis();
//        AppframeClientConfig appframeClientConfig = AppframeClientConfig.getInstance();
//        appframeClientConfig.setAllEndPointUrlByAppWebRootPrefix(appClientHost);
//        //父类转子类
//        User user = DOConverter.getInstance().map(AppframeOrgClient.getInstance().getUserById(Long.parseLong(userId)), User.class);
//        log.info("--[get user time consuming]-- end to get user from appframeClient,wasted {} ms",System.currentTimeMillis() - getUserTime);
//        //缓存到redis 设置超时时间为1小时
//        RedisUtils.saveBean(RedisKeyUtils.getRedisKey(RedisKeyEnum.COMMONS_USER.key(), sha256UserKey), user,
//                RedisConstants.ONE_DAY_S);
//        return user;
//    }
//
//    /**
//     * 获取用户凭证cookie
//     *
//     * @param servletRequest
//     * @return
//     */
//    private String getUserCookie(HttpServletRequest servletRequest) {
//        Cookie[] cookies = servletRequest.getCookies();
//        for (int i = 0; cookies != null && i < cookies.length; i++) {
//            if (CommonConstants.YX_USER_AAGCOOKIE.equals(cookies[i].getName())) {
//                return cookies[i].getValue();
//            }
//        }
//        return null;
//    }
//}
